/*    */ package com.gillion.grule.client.utils;
/*    */ 
/*    */ import groovy.lang.GroovyShell;
/*    */ import groovy.lang.Script;
/*    */ import java.util.Map;
/*    */ import java.util.concurrent.ConcurrentHashMap;
/*    */ import org.apache.commons.codec.digest.DigestUtils;
/*    */ import org.codehaus.groovy.runtime.InvokerHelper;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class GroovyUtils
/*    */ {
/* 17 */   private static final GroovyShell GROOVY_SHELL = new GroovyShell();
/*    */   
/* 19 */   private static final Map<String, Script> SCRIPT_CACHE = new ConcurrentHashMap<>();
/*    */   
/*    */   public static <T> T invokeWithCache(String scriptText, String function, Object... objects) {
/*    */     Script script;
/* 23 */     String cacheKey = DigestUtils.md5Hex(scriptText);
/*    */     
/* 25 */     if (SCRIPT_CACHE.containsKey(cacheKey)) {
/* 26 */       script = SCRIPT_CACHE.get(cacheKey);
/*    */     } else {
/* 28 */       script = GROOVY_SHELL.parse(scriptText);
/* 29 */       SCRIPT_CACHE.put(cacheKey, script);
/*    */     } 
/*    */     
/* 32 */     return (T)InvokerHelper.invokeMethod(script, function, objects);
/*    */   }
/*    */ }
